import 'package:flutter/material.dart';
import 'package:flutterlib/widgets/pull/nested/base_pull_load_manager.dart';
import 'package:flutterlib/widgets/pull/nested/simple_pull_load_page_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'pull_load_widget.dart';

class SimplePullLoadManger extends BasePullLoadManger {
  ///非sliver的listview可以支持头部和空页面，不支持尾部
  SimplePullLoadManger(
      {@required GetDataLogic getDataLogic,
      RenderItem renderItem: _renderItem,
      RenderHeader renderHeader: _renderHeader,
      bool wantKeepAlive: true,
      bool enablePullDown: true,
      bool enablePullUp: true,
      WidgetBuilder renderEmpty})
      : super(
            getDataLogic: getDataLogic,
            renderItem: renderItem,
            renderHeader: renderHeader,
            renderFooter: _renderFooter,
            wantKeepAlive: wantKeepAlive,
            enablePullDown: enablePullDown,
            enablePullUp: enablePullUp,
  renderEmptyView: renderEmpty);

  SimplePullLoadManger.noPullDown(
      {@required GetDataLogic getDataLogic,
      RenderItem renderItem: _renderItem,
      RenderHeader renderHeader: _renderHeader,
      RenderFooter renderFooter: _renderFooter,
      bool wantKeepAlive: true,
      WidgetBuilder renderEmpty})
      : super(
            getDataLogic: getDataLogic,
            renderItem: renderItem,
            renderHeader: renderHeader,
            renderFooter: renderFooter,
            wantKeepAlive: wantKeepAlive,
            enablePullDown: false,
            enablePullUp: false,
  renderEmptyView: renderEmpty);

  SimplePullLoadManger.needSliverHeader(
      {@required GetDataLogic getDataLogic,
      RenderItem renderItem: _renderItem,
      RenderHeader renderHeader: _renderHeader,
      RenderFooter renderFooter: _renderFooter,
      bool wantKeepAlive: true,
      bool enablePullDown: true,
      bool enablePullUp: true,
      WidgetBuilder renderEmpty})
      : super(
            isSliver: true,
            getDataLogic: getDataLogic,
            renderItem: renderItem,
            renderHeader: renderHeader,
            renderFooter: renderFooter,
            wantKeepAlive: wantKeepAlive,
            enablePullDown: enablePullDown,
            enablePullUp: enablePullUp,
            renderEmptyView: renderEmpty);

  SimplePullLoadManger.needSliverHeaderNoPullDown(
      {@required GetDataLogic getDataLogic,
      RenderItem renderItem: _renderItem,
      RenderHeader renderHeader: _renderHeader,
      RenderFooter renderFooter: _renderFooter,
      bool wantKeepAlive: true,
      bool enablePullDown: true,
      bool enablePullUp: true,
      WidgetBuilder renderEmpty})
      : super(
            isSliver: true,
            getDataLogic: getDataLogic,
            renderItem: renderItem,
            renderHeader: renderHeader,
            renderFooter: renderFooter,
            wantKeepAlive: wantKeepAlive,
            enablePullDown: false,
            enablePullUp: false,
            renderEmptyView: renderEmpty);

  static Widget _renderItem(int value, dynamic entity) =>
      Text(value.toString() + entity.toString());

  static List<Widget> _renderHeader(
          BuildContext context, bool innerBoxIsScrolled) =>
      [];

  static List<Widget> _renderFooter(
          BuildContext context, bool innerBoxIsScrolled) =>
      [];

  //控制跳转滚动位置,获取offset
  @override
  getController() {
    return scrollController;
  }

  @override
  void notifyDataSetChange(Function dataChange) {
    dataSetChange.call(dataChange);
  }

  void setNotifyCallBack(void Function(Function callback) notifyDataSetChange) {
    dataSetChange = notifyDataSetChange;
  }

  @override
  void refresh() {
    refreshController?.requestRefresh();
  }

  @override
  getCount() {
    return dataList?.length;
  }

  @override
  void addScrollListener(Function listener) {
    scrollController.addListener(listener);
  }

  @override
  getDataList() {
    return dataList;
  }

  @override
  getRefreshController() {
    return refreshController;
  }

  @override
  void setDataList(List<dynamic> list) {
    dataList = list;
  }

  @override
  void setRefreshController(RefreshController controller) {
    refreshController = controller;
  }

  @override
  getListItem(int i) {
    if (dataList == null) {
      throw Exception("data list is null");
    }
    if (i >= getCount()) {
      throw Exception("num is bigget than list length");
    }
    return dataList[i];
  }

  @override
  animaScrollToPosition(double position, Duration duration, Curve curve) {
    scrollController?.animateTo(position, duration: duration, curve: curve);
  }

  @override
  ensureVisiable(GlobalKey key) {
    Scrollable.ensureVisible(key.currentState.context);
  }

  @override
  scrollToPosition(double position) {
    scrollController?.jumpTo(position);
  }
}
